import { Sequelize } from 'sequelize';
import {
    DB_HOST,
    DB_PORT,
    DB_NAME,
    DB_USER,
    DB_PASSWORD,
} from './constants';
import dotenv from 'dotenv';
import User from '../models/User';
import InterestTag from '../models/InterestTag';
import Circle from '../models/Circle';
import CircleTag from '../models/CircleTag';
import CircleMember from '../models/CircleMember';
import CirclePost from '../models/CirclePost';
import CircleComment from '../models/CircleComment';
import CirclePostLike from '../models/CirclePostLike';
import CircleCommentLike from '../models/CircleCommentLike';

dotenv.config();

export const config = {
    host: process.env.DB_HOST || 'localhost',
    port: parseInt(process.env.DB_PORT || '5432'),
    database: process.env.DB_NAME || 'interest_life_butler',
    username: process.env.DB_USER || 'postgres',
    password: process.env.DB_PASSWORD || 'postgres',
    dialect: 'postgres' as const
};

export const sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASSWORD, {
    host: DB_HOST,
    port: DB_PORT,
    dialect: 'mysql',
    logging: false,
    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000,
    },
    define: {
        timestamps: true,
        underscored: true,
    },
});

// 初始化所有模型
const initModels = () => {
    // 初始化用户模型
    User.init(User.getAttributes(), {
        sequelize,
        modelName: 'User',
        tableName: 'users',
    });

    // 初始化兴趣标签模型
    InterestTag.init(InterestTag.getAttributes(), {
        sequelize,
        modelName: 'InterestTag',
        tableName: 'interest_tags',
    });

    // 初始化圈子相关模型
    Circle.init(Circle.getAttributes(), {
        sequelize,
        modelName: 'Circle',
        tableName: 'circles',
    });

    CircleTag.init(CircleTag.getAttributes(), {
        sequelize,
        modelName: 'CircleTag',
        tableName: 'circle_tags',
    });

    CircleMember.init(CircleMember.getAttributes(), {
        sequelize,
        modelName: 'CircleMember',
        tableName: 'circle_members',
    });

    // 初始化帖子相关模型
    CirclePost.init(CirclePost.getAttributes(), {
        sequelize,
        modelName: 'CirclePost',
        tableName: 'circle_posts',
    });

    CircleComment.init(CircleComment.getAttributes(), {
        sequelize,
        modelName: 'CircleComment',
        tableName: 'circle_comments',
    });

    CirclePostLike.init(CirclePostLike.getAttributes(), {
        sequelize,
        modelName: 'CirclePostLike',
        tableName: 'circle_post_likes',
    });

    CircleCommentLike.init(CircleCommentLike.getAttributes(), {
        sequelize,
        modelName: 'CircleCommentLike',
        tableName: 'circle_comment_likes',
    });
};

export const connectDB = async () => {
    try {
        await sequelize.authenticate();
        console.log('数据库连接成功');

        // 初始化模型
        initModels();

        // 不再需要同步数据库模型，因为我们使用迁移
        // await sequelize.sync();
        console.log('Database models initialized successfully.');
    } catch (error) {
        console.error('数据库连接失败:', error);
        process.exit(1); // 以失败状态退出进程
    }
};

export const testConnection = async () => {
    try {
        await sequelize.authenticate();
        console.log('数据库连接成功');
    } catch (error) {
        console.error('数据库连接失败:', error);
    }
}; 